package com.f_mai.im;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.CreateGroupCallback;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.api.BasicCallback;

import com.tencent.mm.sdk.platformtools.Util;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        JMessageClient.init(getApplicationContext());
        JPushInterface.setDebugMode(true);
    }
    
    public void regist(View view) {
		JMessageClient.register(Util.getDeviceId(this), "123456", new BasicCallback() {
			
			@Override
			public void gotResult(int arg0, String arg1) {
				Toast.makeText(getApplicationContext(), "result:"+arg0+";"+arg1, 1).show();
			}
		});
	}
    public void tochat(View view) {
    	Intent intent = new Intent(getApplicationContext(), ChatActivity.class);
    	intent.putExtra("groupId", 10008652l);
//    	intent.putExtra("targetUser", "A0000043FC17A2");
		startActivity(intent);
    }
    public void login(View view) {
    	JMessageClient.login(Util.getDeviceId(this), "123456", new BasicCallback() {
    		
    		@Override
    		public void gotResult(int arg0, String arg1) {
    			Toast.makeText(getApplicationContext(), "result:"+arg0+";"+arg1, 1).show();
    		}
    	});
    }
    private long groupId;
    public void creatGroup(View view) {
    	JMessageClient.createGroup("first", "第一个群组", new CreateGroupCallback() {
			
			@Override
			public void gotResult(int arg0, String arg1, long arg2) {
				Log.i("baadoo", "result:"+arg0+";"+arg1+";"+arg2);
				groupId = arg2;
			}
		});
    }
    public void addMember(View view) {
    	List<String> mems = new ArrayList<String>();
    	EditText targetEt = (EditText) findViewById(R.id.targetUser);
    	mems.add(targetEt.getText().toString().trim());
    	JMessageClient.addGroupMembers(groupId, mems, new BasicCallback() {
			
			@Override
			public void gotResult(int arg0, String arg1) {
				Toast.makeText(getApplicationContext(), "result:"+arg0+";"+arg1, 1).show();
			}
		});
    }
    public void send(View view) {
    	EditText targetEt = (EditText) findViewById(R.id.targetUser);
    	EditText msgEt = (EditText) findViewById(R.id.msgEt);
    	Message msg = JMessageClient.createSingleTextMessage(targetEt.getText().toString(), msgEt.getText().toString());
    	JMessageClient.sendMessage(msg);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    protected void onResume() {
    	super.onResume();
    	JPushInterface.onResume(this);
    }
    @Override
    protected void onPause() {
    	super.onPause();
    	JPushInterface.onPause(this);
    }
}
